// promise.race方法不会只要状态落定，包装其解决值或者拒绝理由
const myPromiseRace = function(pList) {
    if(!Array.isArray(pList)) {
        return new TypeError('plist必须为数组')
    }
    return new Promise((resolve, reject) => {
        pList.forEach(item => {
            Promise.resolve(item).then(res => {
                resolve(res)
            }).catch(err => {
                reject(err)
            })
        })
    })
}
let p1 = new Promise((res, rej) => {
    setTimeout(() => {
        res('p1')
    }, 1000)
})
let p2 = new Promise((res, rej) => {
    setTimeout(() => {
        res('p2')
    }, 2000)
})
let p3 = new Promise((res, rej) => {
    setTimeout(() => {
        rej('p3')
    }, 1000)
})
let p4 = 'p4'
// myPromiseRace([p1, p2, p3, p4]).then(res => {
//     console.log(res);
// }).catch(err => {
//     console.log(err, '拒绝了');
// })
Promise.race([p1, p2, p3, p4]).then(res => {
    console.log(res, 'resolved');
}).catch(err => {
    console.log(err, 'rejected');
})